﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

using System.IO;

namespace VodafoneContracts
{
    public class CreditConnection : BasicConnection
    {


        public CreditConnection(VodafoneResult result, string username, string password, string number, bool isFirstNumber, Action<HttpWebResponse> basicConnectionCallBack, Action onConnectionError, Action onCompletion)
            : base(result, username, password, number, isFirstNumber, basicConnectionCallBack, onConnectionError, null)
        {
            connections.AddConnection(new ConnectionGET()
            {
                url = "https://www.areaprivati.vodafone.it/190/fads/IlTuoTraffico.do?ls=1&channelId=-18046&tk=9604%2Cl&channelPage=%2FfdtDisp.do%3Factx%3D%2Ffads&pageTypeId=9604&ty_nocache=true&ty_nocache=true",
                CookieContainer = cookieJar,
                prevProcessor = (_) => { },
                currProcessor = (response) =>
                {
                    var sr = new StreamReader(response.GetResponseStream());
                    var html = sr.ReadToEnd();
                    if (html.Contains("disponibile"))
                    {
                        var search_str = "<h4 class=\"petrol bold\">Il tuo traffico disponibile &egrave;: ";
                        int start = html.IndexOf(search_str) + search_str.Length;
                        int end = html.IndexOf(" euro.</h4");
                        result.credit = html.Substring(start, end - start);
                        if(onCompletion!=null)
                            onCompletion();
                    }
                    else
                    {
                        OnConnectionError();
                    }
                },
                onError = OnConnectionError
                
            });
        }
        
    }
}
